package cn.gailvlun.gll.presentation.hotel.night

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.view.ViewGroup
import cn.gailvlun.gll.R
import cn.gailvlun.gll.net.hotel.InsomniaCategory
import cn.gailvlun.gll.net.hotel.InsomniaHotel
import cn.gailvlun.gll.net.hotel.InsomniaType
import cn.gailvlun.gll.presentation.base.App


class HotelBrowsePagerAdapter : FragmentStatePagerAdapter {

    private val titles: Array<String> = App.getInstance().resources.getStringArray(R.array.hotel_browse_tab_title)
    
    private val mType: InsomniaType

    constructor(fm: FragmentManager?, type: InsomniaType) : super(fm) {
        mType = type
    }

    private val fragments = ArrayList<HotelBrowseFragment>()

    override fun getItem(position: Int): Fragment? {
        return when (position) {





            0 -> {
                HotelBrowseFragment.newInstance(InsomniaCategory.S, mType).apply {
                    fragments.add(this)
                }
            }
            1 -> {
                HotelBrowseFragment.newInstance(null, mType).apply {
                    fragments.add(this)
                }
            }
            else -> {
                return null
            }
        }
    }

    override fun getCount() = titles.size

    override fun getPageTitle(position: Int): CharSequence {

        return titles[position]
    }

    private var mCurrentFragment: HotelBrowseFragment? = null

    override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) {
        super.setPrimaryItem(container, position, `object`)
        mCurrentFragment = `object` as HotelBrowseFragment
    }

    fun getCurrentFragment(): HotelBrowseFragment? {
        return mCurrentFragment
    }

    fun getCurrentRecordId(): Int? {
        return mCurrentFragment?.getCurrentRecordId()
    }

    fun changeAllSubscribe(item: InsomniaHotel) {
        (0 until count)
                .map { fragments[it] }
                .forEach { it.changeAllSubscribe(item) }
    }
}